//
//  AddShoppingCartViewController.m
//  基本框架
//
//  Created by runone02 on 2018/8/24.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "AddShoppingCartViewController.h"
#import "ChoseView.h"

@interface AddShoppingCartViewController ()<UITextFieldDelegate> {
    ChoseView *choseView;
    UIView *bgview;
    CGPoint center;
    NSArray *sizearr;//型号数组
    NSArray *colorarr;//分类数组
    NSDictionary *stockdic;//商品库存量
    int goodsStock;
}
@end

@implementation AddShoppingCartViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    [self.navigationController setNavigationBarHidden:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    /**
     这些数据应该从服务器获得 没有服务器我就只能先写死这些数据了
     */
    sizearr = [[NSArray alloc] initWithObjects:@"S",@"M",@"L",@"XL",@"XXL",@"XXXL",@"XXXXL",nil];
    colorarr = [[NSArray alloc] initWithObjects:@"蓝色",@"红色",@"湖蓝色",@"咖啡色",@"黑色",nil];
    NSString *str = [[NSBundle mainBundle] pathForResource: @"stock" ofType:@"plist"];
    stockdic = [[NSDictionary alloc] initWithContentsOfURL:[NSURL fileURLWithPath:str]];
    
    [self initview];
}

#pragma mark-method
- (void)initview{
    /**
     *  商品信息页面内容
     */
    self.view.backgroundColor = [UIColor blackColor];
    //淘宝点击加入购物车时商品信息页面会缩小，中心点上移，背景为黑色，弹出视图为全屏，露出的一部分商品信息页面也有一层很浅的黑色透明视图盖着，所以我对商品信息页面做了以下布局，上导航栏隐藏，self.view作为那个黑色背景，bgview盖在self.view上，所有商品信息都在bgview上，choseView是弹出视图，放在屏幕下方，当点击加入购物车时从下方进入，同时bgview缩小，中心点上移
    bgview = [[UIView alloc] initWithFrame:self.view.bounds];
    bgview.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bgview];
    
    UIButton *btn_add= [UIButton buttonWithType:UIButtonTypeCustom];
    btn_add.frame = CGRectMake(100, 80,200, 50);
    [btn_add setTitleColor:[UIColor whiteColor] forState:0];
    btn_add.titleLabel.font = [UIFont systemFontOfSize:20];
    [btn_add setTitle:@"加入购物车" forState:0];
    btn_add.backgroundColor = [UIColor redColor];
    btn_add.layer.cornerRadius = 4;
    btn_add.layer.borderColor = [UIColor clearColor].CGColor;
    btn_add.layer.borderWidth = 1;
    [btn_add.layer setMasksToBounds:YES];
    [btn_add addTarget:self action:@selector(show) forControlEvents:UIControlEventTouchUpInside];
    [bgview addSubview:btn_add];
    
    [self initChoseView];
}

/**
 *  初始化弹出视图
 */
- (void)initChoseView{
    //选择尺码颜色的视图
    choseView = [[ChoseView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:choseView];
    [choseView.bt_cancle addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    [choseView.bt_sure addTarget:self action:@selector(sure) forControlEvents:UIControlEventTouchUpInside];
    [choseView initTypeView:sizearr :colorarr :stockdic];
    //点击黑色透明视图choseView会消失
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
    [choseView.alphaiView addGestureRecognizer:tap];
}

/**
 *  点击按钮弹出
 */
- (void)show {
    [UIView animateWithDuration: 0.35 animations: ^{
        bgview.transform = CGAffineTransformScale(CGAffineTransformIdentity,0.8,0.8);
        bgview.center = CGPointMake(self.view.center.x, self.view.center.y-50);
        choseView.center = self.view.center;
        choseView.frame =CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    } completion: nil];
}

/**
 *  点击半透明部分或者取消按钮，弹出视图消失
 */
- (void)dismiss {
    center.y = center.y+self.view.frame.size.height;
    [UIView animateWithDuration: 0.35 animations: ^{
        choseView.frame = CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height);
        
        bgview.transform = CGAffineTransformScale(CGAffineTransformIdentity,1.0,1.0);
        bgview.center = self.view.center;
    } completion: nil];
}

- (void)sure {
    [self dismiss];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已经加入购物车" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
    [alert show];
}
@end
